package com.zrwl.poscloud.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.AdminRole;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.AdminRoleMapper;
import com.zrwl.poscloud.xo.service.AdminRoleService;
import com.zrwl.poscloud.xo.service.AdminService;
import com.zrwl.poscloud.xo.vo.AdminRoleVO;
import com.zrwl.poscloud.xo.vo.AdminVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员权限管理 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-04-22
 */
@Service
public class AdminRoleServiceImpl extends SuperServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService {

    @Autowired
    private AdminRoleService adminRoleService;
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private AdminService adminService;

    @Override
    public IPage<AdminRole> getPageList(AdminRoleVO adminRoleVO) {
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(adminRoleVO.getKeyword()) && !StringUtils.isEmpty(adminRoleVO.getKeyword().trim())) {
            queryWrapper.like("name", adminRoleVO.getKeyword().trim());
        }
        Page<AdminRole> page = new Page<>();
        page.setCurrent(adminRoleVO.getCurrentPage());
        page.setSize(adminRoleVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<AdminRole> pageList = adminRoleService.page(page, queryWrapper);
        List<AdminRole> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<AdminRole> getAllList(AdminRoleVO adminRoleVO) {
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<AdminRole> list = adminRoleService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(AdminRoleVO adminRoleVO) {
        AdminRole entity = new AdminRole();
        BeanUtils.copyProperties(adminRoleVO, entity, SysConf.STATUS);
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_uid", adminRoleVO.getAdminUid());
        queryWrapper.eq("role_uid", adminRoleVO.getRoleUid());
        AdminRole temp = adminRoleService.getOne(queryWrapper);
        if (temp == null) {
            entity.setCreateAdminUid(adminService.getMe().getUid());
            entity.insert();
        } else {
            return ResultUtil.errorWithMessage("重复添加!");
        }
        adminRoleVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(AdminRoleVO adminRoleVO) {
        AdminRole entity = adminRoleService.getById(adminRoleVO.getUid());
        BeanUtils.copyProperties(adminRoleVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(AdminRoleVO adminRoleVO) {
        AdminRole entity = adminRoleService.getById(adminRoleVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<AdminRoleVO> getadminRoleList(AdminRoleVO adminRoleVO) {
        return adminRoleMapper.getadminRoleList(adminRoleVO, adminService.getMe().getUid());
    }

    @Override
    public List<AdminRoleVO> getComMerMatchRoleList(String ownerUid) {
        List<AdminRoleVO> adminRoleVOList = new ArrayList<>();
        if (ownerUid != null) {
            adminRoleVOList = adminRoleMapper.getComMerMatchRoleList(ownerUid, adminService.getMe().getUid());
        }
        return adminRoleVOList;
    }

    @Override
    public String adminRoleSwitch(AdminRoleVO adminRoleVO) {
        AdminVO adminVO = adminService.getMe();
        String myAdminUid = adminVO.getUid();
        if (adminRoleVO.getUid() != null && !adminRoleVO.getUid().equals("")) {
            AdminRole entity = adminRoleService.getById(adminRoleVO.getUid());
            if (adminRoleVO.getStatus() == EStatus.ENABLE && !myAdminUid.equals(adminRoleVO.getAdminUid())) {
                entity.setStatus(EStatus.DISABLED);
                //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
                entity.updateById();
            } else if (adminRoleVO.getStatus() == EStatus.DISABLED && !myAdminUid.equals(adminRoleVO.getAdminUid())) {
                entity.setStatus(EStatus.ENABLE);
                //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
                entity.updateById();
            } else {
                return ResultUtil.errorWithMessage("不能关闭自己的权限!");
            }
        } else {
            AdminRole entity = new AdminRole();
            entity.setStatus(EStatus.ENABLE);
            entity.setAdminUid(adminRoleVO.getAdminUid());
            entity.setRoleUid(adminRoleVO.getRoleUid());
            entity.setCreateAdminUid(myAdminUid);
            entity.insert();
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<AdminRole> getmyRoleList(String adminUid) {
        QueryWrapper<AdminRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", EStatus.ENABLE);
        queryWrapper.eq("admin_uid", adminUid);
        List<AdminRole> list = adminRoleService.list(queryWrapper);
        return list;
    }

    @Override
    public List<AdminRoleVO> getMyFullRoleList(String adminUid) {
        AdminRoleVO adminRoleVO = new AdminRoleVO();
        adminRoleVO.setAdminUid(adminUid);
        List<AdminRoleVO> resultList = adminRoleMapper.getMyFullRoleList(adminRoleVO);
        return resultList;
    }

    @Override
    public List<AdminRoleVO> getSuperAdminRoleList(AdminRoleVO adminRoleVO) {
        List<AdminRoleVO> resultList = adminRoleMapper.getSuperAdminRoleList(adminRoleVO);
        String myUid = adminService.getMe().getUid();
        for (AdminRoleVO items : resultList) {
            items.setAdminCheckUid(myUid);
        }
        return resultList;
    }

    @Override
    public String superAdminRoleSwitch(AdminRoleVO adminRoleVO) {
        String myAdminUid = adminService.getMe().getUid();
        AdminRole entity = adminRoleService.getById(adminRoleVO.getUid());
        if (adminRoleVO.getStatus() == EStatus.ENABLE && !myAdminUid.equals(adminRoleVO.getAdminUid())) {
            entity.setStatus(EStatus.DISABLED);
            //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
            entity.updateById();
        } else if (adminRoleVO.getStatus() == EStatus.DISABLED && !myAdminUid.equals(adminRoleVO.getAdminUid())) {
            entity.setStatus(EStatus.ENABLE);
            //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
            entity.updateById();
        } else {
            return ResultUtil.errorWithMessage("不能关闭自己的权限!");
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

}
